package com.waidi.clothespad.adapter;


import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ImageView;

import com.waidi.clothespad.R;

import java.util.LinkedList;

/**
 * Created by ll on 2018/7/5.
 */

public class ListViewVideoAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private LinkedList<ListVideoViewData> mData;
    private int defaultSelection = -1;

    public ListViewVideoAdapter() {}

    public ListViewVideoAdapter(LinkedList<ListVideoViewData> mData, Context mContext) {
        this.mData = mData;
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    public int getSelection()
    {

        return  defaultSelection;
    }

    @Override
    public int getCount() {
        return mData.size();
    }


    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        try
        {

        ViewHolder holder = null;
        if(convertView == null)
        {

            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_video_list,parent,false);
            holder = new ViewHolder();
            holder.img_icon = (ImageView) convertView.findViewById(R.id.iv_play);
            holder.txt_content0 = (TextView) convertView.findViewById(R.id.tv_video_name);
            holder.txt_content1 = (TextView) convertView.findViewById(R.id.tv_video_state);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.img_icon.setImageResource(mData.get(position).getImgId());
        holder.txt_content0.setText(mData.get(position).getNmae());
       // holder.txt_content1.setText(Integer.toString(mData.get(position).getFilesize()));

        if (position == defaultSelection) {// 选中时设置单纯颜色
           // holder.txt_content0.setTextColor("0");
            convertView.setBackgroundColor(Color.rgb(222,233,12));
        } else {// 未选中时设置selector
            //holder.txt_item.setTextColor(colors);
            convertView.setBackgroundColor(Color.TRANSPARENT);
        }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return convertView;
    }

    private class ViewHolder
    {
        ImageView img_icon;
        TextView txt_content0;
        TextView txt_content1;
    }

    /**
     * @param position
     *            设置高亮状态的item
     */
    public void setSelectPosition(int position) {
        if (!(position < 0 || position > mData.size())) {
            defaultSelection = position;
            notifyDataSetChanged();
        }
    }

    public String getCurrentPositiomVideoUrl()
    {
        try
        {
            return  mData.get(defaultSelection).getUrl();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return "";

    }

    public long getCurrentPositiomVideoFileSize()
    {
        try
        {
            return  mData.get(defaultSelection).getFilesize();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return 0;
    }

    public String getCurrentPositiomVideoFileName()
    {
        try
        {
            return  mData.get(defaultSelection).getNmae();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return "";
    }

    public void clearSelectPosition(int position)
    {
        if (!(position < 0 || position > mData.size())) {
            defaultSelection = -1;
            notifyDataSetChanged();
        }
    }
    }